﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using CRMS.Models;
using CRMS.Views;
using CRMS.Views.SelStudy;

namespace CRMS.Views.ClassroomMgnt
{
    public partial class frmClassroomToUse : Form
    {
        private ClassRoomRep cr = new ClassRoomRep();
        private BuildingRep bRep = new BuildingRep();
        private string bName;
        private int roomID = 0;
        public frmClassroomToUse(string bName,int rID)
        {
            InitializeComponent();
            this.position = 0;
            this.bName = bName;
            roomID = rID;
        }
        public frmClassroomToUse()
        {
            InitializeComponent();
            this.position = 0;
        }
    

        private void cboxQueryStyle_SelectedValueChanged(object sender, EventArgs e)
        {
            MessageBox.Show("还没有实现呢，帅哥~");
        }

        private void frmClassroomToUse_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            if (roomID != 0)
            {
                dataGridView1.DataSource = cr.getCRMByBCID(bName,roomID);
            }
            else
            {
                dataGridView1.DataSource = cr.getToUseClassroom();
            }
        }

        private void tsbtnFirstItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position != 0)
                {
                    foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.dataGridView1.Rows[0].Selected = true;
                    this.position = 0;
                }
            }

        }

        private void tsbtnUpItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position > 0)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position--;
                    this.dataGridView1.Rows[this.position].Selected = true;
                    if (this.position == 0)
                    {
                        MessageBox.Show("当前已是第一条记录！");
                    }

                }
            }
        }

        private void tsbtnNextItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position < this.dataGridView1.Rows.Count - 1)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position++;
                    this.dataGridView1.Rows[this.position].Selected = true;
                    if (this.position == this.dataGridView1.Rows.Count - 1)
                    {
                        MessageBox.Show("当前已是最后一条记录！");
                    }
                }
            }
        }

        private void tsbtnLastItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position != this.dataGridView1.Rows.Count - 1)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position = this.dataGridView1.Rows.Count - 1;
                    this.dataGridView1.Rows[this.position].Selected = true;
                }
            }
        }

        private void btnRetrive_Click(object sender, EventArgs e)
        {
            MessageBox.Show("还没有实现呢!");
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtBuildingName.Text = bRep.GetBuidingByID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString()).TeachingBuildingName;
            txtRoomID.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            txtUseDate.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            txtWeekday.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            txtPeriod.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            txtUseStatus.Text = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            
        }

        private void NotApproval_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = cr.getToUseClassroom().Where(x => x.UseStatus == '0');
            this.Approval.Visible = true;
            this.Cancel.Visible = false;
        }

        private void HadApproval_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = cr.getToUseClassroom().Where(x => x.UseStatus == '1');
            this.Approval.Visible = false;
            this.Cancel.Visible = true;
        }

        private void Approval_Click(object sender, EventArgs e)
        {
            ToUseClassroom tuc = new ToUseClassroom();
            tuc = cr.getOneInstance(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                                    Int32.Parse(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString()),
                                    dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                                    dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                                    dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            if (tuc == null)
            {
                MessageBox.Show("获取失败");
            }
            else
            {
                tuc.UseStatus = '1';
                cr.Save();
                this.NotApproval_Click(sender, e);
            }
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            ToUseClassroom tuc = new ToUseClassroom();
            tuc = cr.getOneInstance(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                                    Int32.Parse(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString()),
                                    dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                                    dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                                    dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            if (tuc == null)
            {
                MessageBox.Show("获取失败");
            }
            else
            {
                tuc.UseStatus = '0';
                cr.Save();
                this.HadApproval_Click(sender, e);
            }
        }

        
    }
}
